package com.mars.common;

import com.mars.pojo.SysUser;

import javax.servlet.http.HttpServletRequest;

/**
 * ThreadLocal的应用，每个进程会处理自己的内容，对高并发有用
 * @author MARS
 * @date 2018/1/14
 */
public class RequestHolder {

    private static final ThreadLocal<SysUser> userHolder = new ThreadLocal<SysUser>();
    private static final ThreadLocal<HttpServletRequest> requestHolder = new ThreadLocal<HttpServletRequest>();

    public static void add(SysUser sysUser) {
        userHolder.set(sysUser);
    }

    public static void add(HttpServletRequest request) {
        requestHolder.set(request);
    }

    /**
     * 获取sysUser
     */
    public static SysUser getCurrentUser() {
        return userHolder.get();
    }

    public static HttpServletRequest getCurrentRequest() {
        return requestHolder.get();
    }

    /**
     *
     * 移除,否则会造成内存泄露
     */

    public static  void remove() {
        userHolder.remove();
        requestHolder.remove();
    }
}
